$(document).ready(function(){
	$('#product_size_form').submit(function(){
		return false;
	});
	$('#btn_add_product_size').click(function(){
		addproduct_size();
	});
	$('#btn_update_product_size').click(function(){
		updateproduct_size();
	});
	$('#btn_delete_product_size').click(function(){
		deleteproduct_size();
	});
	$('#btn_clear').click(function(){
		$('#product_size_size_name').val('');
		$('#product_size_hidden_id').val(0);
		$('#category_level_2').val(0);
		$('#type_of_size').val(0);
		$('#product_size_id').val('');
		$('#submit_error').html('');
	});
	$('#btn_search_product_size').click(function(){
		getproduct_size();
	});
	$('#btn_search_size_name').click(function(){
		searchSize();
	});
});

function searchSize(){
	$('#submit_error').html('');
	var data = {
		keyword: $('#search_size_name').val()
	};
	$('#search_result').html('');
	var request = {
		url: base_url + SEARCH_PRODUCT_SIZE,
		type: 'POST',
		data: data,
		success: function(data){
			if(data){
				$('#search_result').html(data);
				$('#product_size_table').dataTable({
					"bPaginate": true,
					"bLengthChange": false,
					"bFilter": false,
					"bSort": true,
					"bInfo": true,
					"bAutoWidth": false
				});
			} else {
				$('#submit_error').html(ERROR_ID_NOT_FOUND);
			}
		},
		error: function(error){
			$('#submit_error').html(ERROR_ID_NOT_FOUND);
		}
	};
	excuteAjax(request);
}

function getproduct_size(){
	var product_size_id = parseInt($('#product_size_id').val());
	if(product_size_id){
		$('#submit_error').html('');
		var data = {
			id: product_size_id
		};
		var request = {
			url: base_url + GET_PRODUCT_SIZE,
			type: 'POST',
			data: data,
			success: function(data){
				if(data){
					data = JSON.parse(data);
					$('#category_level_2').val(data.category_level_2_id);
					$('#type_of_size').val(data.type_of_size_id);
					$('#product_size_size_name').val(data.size_name);
					$('#product_size_hidden_id').val(data.id);
				} else {
					$('#submit_error').html(ERROR_ID_NOT_FOUND);
				}
			},
			error: function(error){
				$('#submit_error').html(ERROR_ID_NOT_FOUND);
			}
		};
		excuteAjax(request);
	} else {
		$('#submit_error').html(ERROR_INVALID_ID);
	}
}

function addproduct_size(){
	var category_level_2 = $('#category_level_2').val();
	var type_of_size = $('#type_of_size').val();
	var product_size_size_name = $('#product_size_size_name').val();
	$('#submit_error').html('');
	if(product_size_size_name !== '' && category_level_2 !=0  && type_of_size != 0){
		var data = {
			category_level_2_id: category_level_2,
			type_of_size_id: type_of_size,
			size_name: product_size_size_name
		};
		var request = {
			url: base_url + ADD_PRODUCT_SIZE,
			type: 'POST',
			data: data,
			success: function(data){
				if(data == '1'){
					$('#submit_error').html(RESPONSE_SUCCESS);
				} else {
					$('#submit_error').html(RESPONSE_ERROR);
				}
			},
			error: function(error){
				$('#submit_error').html(RESPONSE_ERROR);
			}
		};
		excuteAjax(request);
	} else {
		$('#submit_error').html(ERROR_INPUT);
	}
	$("html, body").animate({ scrollTop: 0 }, "slow");
}

function updateproduct_size(){
	var product_size_id = $('#product_size_hidden_id').val();
	var category_level_2 = $('#category_level_2').val();
	var type_of_size = $('#type_of_size').val();
	var product_size_size_name = $('#product_size_size_name').val();
	$('#submit_error').html('');
	if(product_size_size_name !== '' && product_size_id != 0 && category_level_2 != 0 && type_of_size != 0){
		var data = {
			id: product_size_id,
			category_level_2_id: category_level_2,
			type_of_size_id: type_of_size,
			size_name: product_size_size_name
		};
		var request = {
			url: base_url + UPDATE_PRODUCT_SIZE,
			type: 'POST',
			data: data,
			success: function(data){
				if(data == '1'){
					$('#submit_error').html(RESPONSE_SUCCESS);
				} else {
					$('#submit_error').html(RESPONSE_ERROR);
				}
			},
			error: function(error){
				$('#submit_error').html(RESPONSE_ERROR);
			}
		};
		excuteAjax(request);
	} else {
		$('#submit_error').html(ERROR_INPUT);
	}
	$("html, body").animate({ scrollTop: 0 }, "slow");
}

function deleteproduct_size(){
	var product_size_id = $('#product_size_hidden_id').val();
	$('#submit_error').html('');
	if(product_size_id != 0){
		var data = {
			id: product_size_id
		};
		var request = {
			url: base_url + DELETE_PRODUCT_SIZE,
			type: 'POST',
			data: data,
			success: function(data){
				if(data == '1'){
					$('#submit_error').html(RESPONSE_SUCCESS);
				} else {
					$('#submit_error').html(RESPONSE_ERROR);
				}
			},
			error: function(error){
				$('#submit_error').html(RESPONSE_ERROR);
			}
		};
		excuteAjax(request);
	} else {
		$('#submit_error').html(ERROR_INPUT);
	}
	$("html, body").animate({ scrollTop: 0 }, "slow");
}